package ph.gov.marikina.mcsmtms.tests;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import ph.gov.marikina.mcsmtms.constants.GlobalConstants.Direction;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.ReportSource;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.Roads;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.SituationTypes;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.Status;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.TrafficConditionTypes;
import ph.gov.marikina.mcsmtms.dao.SituationReportDAO;
import ph.gov.marikina.mcsmtms.domain.MobileUserReport;
import ph.gov.marikina.mcsmtms.domain.SituationReport;
import ph.gov.marikina.mcsmtms.domain.SituationSegment;
import ph.gov.marikina.mcsmtms.rest.webservices.client.BasicSecureSimpleClientHttpRequestFactory;
import ph.gov.marikina.mcsmtms.rest.webservices.client.SimpleCredentials;
import ph.gov.marikina.mcsmtms.rest.webservices.client.SimpleCrendentialsProvider;
import ph.gov.marikina.mcsmtms.services.SituationReportService;
import ph.gov.marikina.mcsmtms.utils.Utils;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;


public class SituationReportServiceTest {

	Logger log = Logger.getLogger(SituationReportServiceTest.class);

	/** The application context. */
	private static ApplicationContext springContext = new ClassPathXmlApplicationContext(
			"spring/test-spring-config.xml");

	@Before
	public void setUp() throws Exception {
		
	}

	@Test
	public void listSituationCounts() {
		SituationReportService situationReportService = (SituationReportService) springContext
				.getBean("situationReportService");
		
		Date startDate = Utils.parseDate("01/01/2013");
		Date endDate = Utils.parseDate("12/31/2013");
		
        Map<String, Map<String, Integer>> situationCountsMap = situationReportService.getSituationCountsForDateRange(startDate, endDate);
        
        
        assertNotNull(situationCountsMap);
        assertTrue(situationCountsMap.size() > 0);
        for(String key : situationCountsMap.keySet()) {
        	System.out.println(key);
        	System.out.println("------");
        	Map<String, Integer> situationTypeCountsMap = situationCountsMap.get(key);
        	
        	for(String situationTypeKey : situationTypeCountsMap.keySet()) {
        		System.out.println(situationTypeKey + " = " + situationTypeCountsMap.get(situationTypeKey));
        	}
        }
	}
	
	@Test
	public void listFrequentlyFloodedRoads() {
		SituationReportService situationReportService = (SituationReportService) springContext
				.getBean("situationReportService");
		
		Date startDate = Utils.parseDate("01/01/2013");
		Date endDate = Utils.parseDate("12/31/2013");
		
        List<Map<String, Object>> floodedReportList = situationReportService.getFrequentlyFloodedStreets(startDate, endDate);
        
        assertNotNull(floodedReportList);
        assertTrue(floodedReportList.size() > 0);
        for(Map<String, Object> floodReportMap : floodedReportList) {
        	System.out.println(floodReportMap.get("road") + " " + floodReportMap.get("fromDate") + "  " + floodReportMap.get("toDate") + "  " + floodReportMap.get("interval"));
        }
	}
	
	@Test
	public void listMostCongestedRoads() {
		SituationReportService situationReportService = (SituationReportService) springContext
				.getBean("situationReportService");
		
		Date startDate = Utils.parseDate("01/01/2013");
		Date endDate = Utils.parseDate("12/31/2013");
		
        List<Map<String, Object>> mostCongestedStreetList = situationReportService.getMostCongestedStreets(startDate, endDate);
        
        assertNotNull(mostCongestedStreetList);
        assertTrue(mostCongestedStreetList.size() > 0);
        for(Map<String, Object> mostCongestedStreetMap : mostCongestedStreetList) {
        	System.out.println(mostCongestedStreetMap.get("road") + " " + mostCongestedStreetMap.get("fromDate") + "  " + mostCongestedStreetMap.get("toDate") + "  " + mostCongestedStreetMap.get("interval") + " " + mostCongestedStreetMap.get("situation"));
        }
	}
	
	@Test
	public void testAddSituationReport() {
		SituationReportService situationReportService = (SituationReportService) springContext
				.getBean("situationReportService");
		
		SituationReport situationReport = new SituationReport();
		
		situationReport.setRoad(Roads.BAYAN_BAYANAN.getCode());
		situationReport.setDirection(Direction.A_TO_B.getCode());
		situationReport.setLastUpdateDateTime(new Date());
		situationReport.setReporter(18);
		situationReport.setReportSource(ReportSource.WEB.getCode());
		situationReport.setStatus(Status.VALIDATED.getCode());
		
		SituationSegment situationSegment = new SituationSegment();
		situationSegment.setSituationReport(situationReport);
		situationSegment.setSituation(SituationTypes.ACCIDENT.getCode());
		situationSegment.setAccessibleYn(true);
		situationSegment.setTrafficCondition(TrafficConditionTypes.HEAVY.getCode());
		situationSegment.setDescription("Test");
		situationSegment.setStartLat(new Float(0.0));
		situationSegment.setStartLng(new Float(0.0));
		situationSegment.setEndLat(new Float(0.0));
		situationSegment.setEndLng(new Float(0.0));
		//situationSegment.setSituationReportId(situationReport.getId());
		
		List<SituationSegment> situationSegmentList = new ArrayList<SituationSegment>();
		situationSegmentList.add(situationSegment);
		situationReport.setSituationSegmentList(situationSegmentList);
		
		situationReportService.addSituationReport(situationReport);
	}


}

